#include <stdio.h>
#include <string.h>
#include <curl/curl.h>
#include <stdlib.h>
char jsonid[128];
char key[32];
char card_key[32];

int error_flag;

long login_index(void *data, int size, int nmemb, char* content)
{
	char *p;
	p = strstr(data,"KEY");
	strncpy(key,p+3, 13);
	return size;
}

struct curl_slist* getUrl(char *filename)
{
	CURL *curl;
	CURLcode res;
	struct curl_slist *cookies;
	char buf[2056];
	char cookie[256];
	double len;
	char *ret_p;
	char *p ;
	struct curl_slist *headers = NULL;

	headers = curl_slist_append(headers, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	headers = curl_slist_append(headers, "Accept-Language: en-US,en;q=0.5");
	headers = curl_slist_append(headers, "Accept-Encoding: gzip, deflate");
	headers = curl_slist_append(headers, "Connection: keep-alive");
	curl = curl_easy_init();    // 初始化
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
		curl_easy_setopt(curl, CURLOPT_URL,"http://kq.neusoft.com");
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION , login_index);
		curl_easy_setopt(curl, CURLOPT_COOKIELIST, cookie);
		res = curl_easy_perform(curl);   // 执行
		curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE , &len);
		printf("len[%lf]\n",len);
		curl_easy_getinfo(curl, CURLINFO_CONTENT_TYPE, &ret_p);
		curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &len);
		curl_easy_getinfo(curl, CURLINFO_COOKIELIST, &cookies);
		printf("type[%s]\n",ret_p);
		printf("buf = %p",buf);
		printf("len[%lf]\n",len);
		printf("cookies[%p]\n",cookies);

		p = strrchr(cookies->data, '\t');
		printf("file:%s,line:%d,func:%s,jsonid[%p],p[%s]\n",__FILE__,__LINE__,__func__,jsonid,p); 
		strcpy(jsonid , p+1);
		printf("json=%s,key=%s",jsonid,key);
		curl_easy_cleanup(curl);
		return cookies;
	}
}


long login_return(void *data, int size, int nmemb, char* content)
{
	printf("return =%s",data);
	char *p = strstr(data,"error=3");
	if(p){
		printf("file:%s,line:%d,func:%s,error=3!!!\n",__FILE__,__LINE__,__func__);
		error_flag = 1;
	}
	return size;
}

int postUrl(struct curl_slist *cookies,char *post_content)
{	
	CURL *curl;
	CURLcode res;
	char jsonbuf[128] = "JSESSIONID=";
	strcat(jsonbuf, jsonid);
	curl = curl_easy_init();    // 初始化
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_URL, "http://kq.neusoft.com/login.jsp");   // 指定url
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_content);    // 指定post内容
		curl_easy_setopt(curl, CURLOPT_COOKIE, jsonbuf);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION , login_return);
		res = curl_easy_perform(curl);
		printf("file:%s,line:%d,func:%sres[%d]\n",__FILE__,__LINE__,__func__,res); 
		curl_easy_cleanup(curl);
	}
	return 0;
}

void build_login_contant(char *post_content,char *account, char* password)
{
	char *json_key;

	json_key = strchr(jsonid, '!');
	*json_key = '\0';
	strcpy(post_content, "login=true&neusoft_attendance_online=&KEY");
	strcat(post_content, key);
	strcat(post_content, "=&neusoft_key=ID");
	strcat(post_content, key);
	strcat(post_content, "PWD");
	strcat(post_content, key);
	strcat(post_content, "&ID");
	strncat(post_content, jsonid, 52);
	strcat(post_content, "%21");
	strcat(post_content, json_key +1);
	strcat(post_content,"%21");
	strcat(post_content, key);
	strcat(post_content, "=");
	strcat(post_content, account);
	strcat(post_content, "&KEY");
	strcat(post_content, jsonid);
	strcat(post_content,"%21");
	strcat(post_content, json_key + 1);
	strcat(post_content, "%21");
	strcat(post_content, key);
	strcat(post_content, "=");
	strcat(post_content, password);
	*json_key = '!';
}

long login_move_p(void *data, int size, int nmemb, char* content)
{
	char *p = strstr(data,"currentempoid");
	char *p2 = strstr(p,"value");
	char *p3 = strchr(p2,'"');
	strncpy(card_key,p3+1,10);
	//printf("%s",data);
	
	return size;
}

int move_url( void )
{
	CURL* curl;
	CURLcode res;
	struct curl_slist *cookies;
	char buf[2056];
	char cookie[256];
	double len;
	char *ret_p;
	char *p ;
	struct curl_slist *headers = NULL;
	char jsonbuf[128] = "JSESSIONID=";
	strcat(jsonbuf, jsonid);
	headers = curl_slist_append(headers, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
	headers = curl_slist_append(headers, "Accept-Language: en-US,en;q=0.5");
	headers = curl_slist_append(headers, "Accept-Encoding: gzip, deflate");
	headers = curl_slist_append(headers, "Connection: keep-alive");
	curl = curl_easy_init();    // 初始化
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);// 改协议头
		curl_easy_setopt(curl, CURLOPT_URL,"http://kq.neusoft.com/attendance.jsp");
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION , login_move_p);
		curl_easy_setopt(curl, CURLOPT_COOKIE, jsonbuf);
		printf("file:%s,line:%d,func:%s\n",__FILE__,__LINE__,__func__); 
		res = curl_easy_perform(curl);   // 执行
		curl_easy_cleanup(curl);
	}
}

void post_card(char *buf, char* html)
{
	CURL *curl;
	CURLcode res;
	char jsonbuf[128] = "JSESSIONID=";
	strcat(jsonbuf, jsonid);
	char url[256]="http://kq.neusoft.com/";
	strcat(url, html);
	curl = curl_easy_init();    // 初始化
	if (curl)
	{
		curl_easy_setopt(curl, CURLOPT_URL, url);   // 指定url
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, buf);    // 指定post内容
		curl_easy_setopt(curl, CURLOPT_COOKIE, jsonbuf);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION , login_return);
		res = curl_easy_perform(curl);
		printf("file:%s,line:%d,func:%sres[%d]\n",__FILE__,__LINE__,__func__,res); 
		curl_easy_cleanup(curl);
	}
	return ;
}

int main(int argc, char** argv)
{
	
	struct curl_slist *cookies;
	char post_content[2048];
	char buf[256];
	
	unsigned int rindex = 0;
restart:
	bzero(post_content, sizeof(post_content));
	bzero(buf,sizeof(buf));
	strcpy(buf,"currentempoid=");
	error_flag = 0;
	if(rindex > 2){
		exit(0);
	}
	rindex++;
	cookies = getUrl("/tmp/get.html");
	sleep(1);
	build_login_contant(post_content,argv[1],argv[2]);
	printf("contetn = %s",post_content);
	postUrl(cookies,post_content);
	if(error_flag == 1){
		bzero(jsonid, sizeof(jsonid));
		bzero(key, sizeof(key));
		bzero(card_key,sizeof(card_key));
		goto restart;
	}
	move_url();
	strcat(buf,card_key);
	post_card(buf,"record.jsp");
	
	printf("file:%s,line:%d,func:%s,card[%s]\n",__FILE__,__LINE__,__func__,card_key);
	printf("file:%s,line:%d,func:%s,buf[%s]\n",__FILE__,__LINE__,__func__,buf); 
	post_card(buf,"close.jsp");
	return 0;
}
